﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Pavilion;

namespace Pavilion.GameTools
{
    public class EnvironmentTool : GameToolBase
    {
        private List<ImageLayer> backgroundLayersList;
        private DrawVisibilityGroup drawVisibilityGroup;
        static public MultiLinePhysicalData MultiLineData { get; private set; }

        public EnvironmentTool()
        {
            backgroundLayersList = new List<ImageLayer>();
            MultiLineData = new MultiLinePhysicalData();
        }

        public override void Initialize(GameItem gameItem)
        {
            drawVisibilityGroup = gameItem.DrawVisibilityGroup;

            gameItem.Disposed += () =>
            {
                foreach (ImageLayer imageLayer in backgroundLayersList)
                    imageLayer.Dispose();

                backgroundLayersList.Clear();
            };

            CollisionManager.Instance.AddMultiLineData(MultiLineData, (p) =>
            {
                foreach (PolygonPhysicalData polygonData in p.InteresectedPolygons)
                {
                    //if (polygonData.PhysicalBounds.Rectangle.Bottom 
                }
            });

            DrawItem drawItem = new DrawItem(50, () =>
            {
                foreach (List<Vector2> lineSequence in MultiLineData.LineSequenceCollection)
                {
                    for (int i = 1; i < lineSequence.Count; i++)
                        DrawManager.Instance.DrawLine(lineSequence[i-1], lineSequence[i], Color.Black);
                }
            });
            DrawManager.Instance.Add(drawItem);
            drawVisibilityGroup.Add(drawItem);
        }

        public override void Deserialize(XElement xmlProperties)
        {
            string resourceName = xmlProperties.Attribute("ResourceName").Value;
            string directoryPath = GameEngine.GameResourcesDirectory + @"Environments\" + resourceName;

            XElement scriptNode = XDocument.Load(directoryPath + @"\script.xml").Element("script");
            IEnumerable<XElement> xmlLayerList = scriptNode.Element("LayerList").Elements("Layer");

            foreach (XElement layerNode in xmlLayerList)
                CreateImageLayer(layerNode, directoryPath + @"\Built Data");

            XElement environmentLineListNode = scriptNode.Element("EnvironmentLineList");

            if (environmentLineListNode != null)
            {
                IEnumerable<XElement> xmlLineList = environmentLineListNode.Elements("Line");

                foreach (XElement lineNode in xmlLineList)
                    CreateLineSequence(lineNode.Attribute("Sequence").Value);
            }
        }

        private void CreateLineSequence(string lineSequenceValue)
        {
            List<Vector2> lineSequence = new List<Vector2>();

            foreach (string point in lineSequenceValue.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
            {
                string[] pointValues = point.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                lineSequence.Add(new Vector2(float.Parse(pointValues[0]), float.Parse(pointValues[1])));
            }

            MultiLineData.LineSequenceCollection.Add(lineSequence);
        }

        public override void Serialize()
        {
            throw new NotImplementedException();
        }

        private void CreateImageLayer(XElement layerNode, string directoryPath)
        {
            ImageLayer imageLayer = new ImageLayer(drawVisibilityGroup);
            imageLayer.Deserialize(layerNode);
            imageLayer.Load(directoryPath);

            backgroundLayersList.Add(imageLayer);
        }
    }
}
